//2024-5-26
#include<iostream>
using namespace std;
struct ListNode
{
	ListNode* _next;
	int _val;

	ListNode(int val)
		:_next(nullptr)
		, _val(val)
	{}
};

struct ListNode* CreateListNode(int val)
{
	struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return NULL;
	}

	newnode->_next = NULL;
	newnode->_val = val;
	return newnode;
}

// 创建的不带哨兵位
ListNode* CreateList(int n)
{
	ListNode head(-1);  // 哨兵位

	ListNode* tail = &head;
	int val;
	printf("请依次输入%d个节点的值：>", n);
	for (size_t i = 0; i < n; i++)
	{
		cin >> val;
		tail->_next = new ListNode(val);
		tail = tail->_next;
	}

	return head._next;
}

void func()
{
	int n = 1;
	while (1)
	{
		int* p = new int[1024 * 1024 * 100];
		//int* p = (int*)malloc(1024 * 1024*4);

		cout << n << "->" << p << endl;
		++n;
	}
}

int main()
{
	// 1、用法上，变简洁了
	int* p0 = (int*)malloc(sizeof(int));
	int* p1 = new int;
	int* p2 = new int[10]; // new 10个int对象

	// 2、可以控制初始化
	int* p3 = new int(10); // new 1个int对象,初始化成10
	int* p4 = new int[10]{ 1,2,3,4,5 };

	// 3、自定义类型，开空间+构造函数
	// 4、new失败了以后抛异常，不需要手动检查
	ListNode* node1 = new ListNode(1);
	ListNode* node2 = new ListNode(2);
	ListNode* node3 = new ListNode(3);
	//...
	//ListNode* list1 = CreateList(5);

	delete p3;
	delete[] p4;
	delete p1;
	delete[] p2;

	try
	{
		func();
	}
	catch (const exception& e)
	{
		cout << e.what() << endl;
	}

	return 0;
}